package com.atguigu.classroom.vod.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.classroom.exception.ClassroomException;
import com.atguigu.classroom.model.vod.Subject;
import com.atguigu.classroom.vo.vod.SubjectEeVo;
import com.atguigu.classroom.vod.listener.SubjectListener;
import com.atguigu.classroom.vod.mapper.SubjectMapper;
import com.atguigu.classroom.vod.service.SubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Override
    public List<Subject> getSubjects(Long id) {
        LambdaQueryWrapper<Subject> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Subject::getParentId,id);
        List<Subject> subjectList = subjectMapper.selectList(queryWrapper);
        if(subjectList.size()==0){
            return subjectList;
        }
        //做第二次sql，判断是否有子课程
        List<Long> idList=new ArrayList<>();
        idList=subjectList.stream().map(subject->{
            return subject.getId();
        }).collect(Collectors.toList());
        LambdaQueryWrapper<Subject> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(Subject::getParentId,idList);
        List<Subject> subjectChildrenList=subjectMapper.selectList(queryWrapper1);
        Map<Long,Boolean> subjectMap=new HashMap<>();
        for(Subject subject:subjectChildrenList){
            subjectMap.put(subject.getParentId(),true);
        }
        for(Subject subject:subjectList){
            subject.setHasChildren(subjectMap.containsKey(subject.getId()));
        }
        return subjectList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName =URLEncoder.encode("课程分类", "UTF-8");
            //这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        response.setHeader("Content-disposition", "attachment;filename="+fileName +".xlsx");
        List<Subject> subjectList = subjectMapper.selectList(null);//查询所有课程数据
        List<SubjectEeVo> subjectEeVoList=subjectList.stream().map(subject -> {
            SubjectEeVo subjectEeVo=new SubjectEeVo();
            BeanUtils.copyProperties(subject,subjectEeVo);
            return subjectEeVo;
        }).collect(Collectors.toList());
        EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
                .sheet("课程分类表").doWrite(subjectEeVoList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @Autowired
    private SubjectListener subjectListener;
    @Override
    public void importData(MultipartFile multipartFile) {
        try {
            EasyExcel.read(multipartFile.getInputStream(),SubjectEeVo.class,subjectListener).sheet().doRead();
            this.saveBatch(subjectListener.subjectList);
        } catch (IOException ioException) {
            throw new ClassroomException();
        }
    }
}
